package cn.com.backstage.controller.base;

import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import cn.com.api.entity.ApiT_city;
import cn.com.api.entity.ApiT_user_admin;
import cn.com.api.service.ApiT_cityService;
import cn.com.api.service.ApiT_user_adminService;
import cn.com.util.*;
import cn.com.util.*;


public class T_cityBaseController extends BaseController {

	private static Logger logger = Logger.getLogger(T_cityBaseController.class);
	
	@Autowired
	private ApiT_cityService apiT_cityService;
	@Autowired
	private ApiT_user_adminService apiT_user_adminService;
	 
	/**
	 * 列表
	 * @return
	 */
	@RequestMapping(value = "t_city-list")
	public ModelAndView t_city_list(HttpServletRequest request, HttpServletResponse response) throws Exception{
		ModelAndView mv = new ModelAndView("forward:/backstage/t_city_list.jsp");
		
		List<ApiT_city> t_citys = apiT_cityService.findT_citys();
		mv.addObject("t_citys", t_citys);
		
		return mv;
	}
	
	/**
	 * 进入新增或者编辑页面
	 * @return
	 */
	@RequestMapping(value = "t_city-add-pre")
	public ModelAndView t_city_add_pre(HttpServletRequest request, HttpServletResponse response) throws Exception{
		ModelAndView mv = new ModelAndView("forward:/backstage/t_city_add.jsp");
		//
		if(request.getParameter("id")!=null){
			String id = request.getParameter("id");
			ApiT_city t_city = apiT_cityService.findT_cityById(Integer.valueOf(id));
			ApiT_user_admin admin = apiT_user_adminService.getStationRoleAdmin(Integer.valueOf(id));
			if (admin != null){
				t_city.setReserve_money(admin.getReserve_money());
			}
			mv.addObject("t_city",t_city);
		}
		return mv;
	}

	/**
	 *新增或者编辑
	 * @return
	 */
	@RequestMapping(value = "t_city-add")
	public void t_city_add(HttpServletRequest request, HttpServletResponse response) throws Exception{
		
		String name = request.getParameter("name");
		String uname = request.getParameter("uname");
		String is_effect = request.getParameter("is_effect");
		String is_delete = request.getParameter("is_delete");
		String pid = request.getParameter("pid");
		String is_open = request.getParameter("is_open");
		String is_default = request.getParameter("is_default");
		String description = request.getParameter("description");
		String notice = request.getParameter("notice");
		String seo_title = request.getParameter("seo_title");
		String seo_keyword = request.getParameter("seo_keyword");
		String seo_description = request.getParameter("seo_description");
		String sort = request.getParameter("sort");
		String u_name = request.getParameter("u_name");
		String is_hot = request.getParameter("is_hot");
		String longitude = request.getParameter("longitude");
		String latitude = request.getParameter("latitude");
		String send_distance = request.getParameter("send_distance");
		String open_time = request.getParameter("open_time");
		String address = request.getParameter("address");
		String contacts = request.getParameter("contacts");
		String tel = request.getParameter("tel");
		
		Map<String,Object> map = new HashMap<String,Object>();
		String id = request.getParameter("id");
		//add
		if(id==null || (id.trim()).equals("")){
			ApiT_city t_city = new ApiT_city();
            
		    //t_city.setId(id);
		    t_city.setName(name);
		    t_city.setUname(uname);
		    t_city.setIs_effect(1);
		    t_city.setIs_delete(0);
		    //t_city.setPid(pid);
		    t_city.setIs_open(1);
		    t_city.setIs_default(0);
		    t_city.setDescription(description);
		    t_city.setNotice(notice);
		    t_city.setSort(0);
		    t_city.setU_name(u_name);
		    t_city.setIs_hot(0);
		    t_city.setLongitude(Float.valueOf(longitude));
		    t_city.setLatitude(Float.valueOf(latitude));
		    t_city.setSend_distance(Float.valueOf(send_distance));
		    t_city.setOpen_time(open_time);
		    t_city.setAddress(address);
		    t_city.setContacts(contacts);
		    t_city.setTel(tel);
	        
			apiT_cityService.insertT_city(t_city);
		}
		//edit
		else{
			ApiT_city t_city = apiT_cityService.findT_cityById(Integer.valueOf(id));
		    //t_city.setId(id);
		    t_city.setName(name);
		    t_city.setUname(uname);
		    t_city.setIs_effect(1);
		    t_city.setIs_delete(0);
		    //t_city.setPid(pid);
		    t_city.setIs_open(1);
		    t_city.setIs_default(0);
		    t_city.setDescription(description);
		    t_city.setNotice(notice);
		    t_city.setSort(0);
		    t_city.setU_name(u_name);
		    t_city.setIs_hot(0);
		    t_city.setLongitude(Float.valueOf(longitude));
		    t_city.setLatitude(Float.valueOf(latitude));
		    t_city.setSend_distance(Float.valueOf(send_distance));
		    t_city.setOpen_time(open_time);
		    t_city.setAddress(address);
		    t_city.setContacts(contacts);
		    t_city.setTel(tel);
			apiT_cityService.updateT_city(t_city);
		}
		//
		map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
		map.put(Utils.MESSAGE, "添加成功！");
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.print(JsonUtil.object2json(map));
		out.close();
	}
	
	/**
	 *删除
	 * @return
	 */
	@RequestMapping(value = "t_city-delete")
	public void t_city_delete(HttpServletRequest request, HttpServletResponse response) throws Exception{
		
		Map<String,Object> map = new HashMap<String,Object>();
		String id = request.getParameter("id");
		
		//delete
		int ret_i =	apiT_cityService.deleteT_city(Integer.valueOf(id));
		
		//
		if(ret_i > 0){
			map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
			map.put(Utils.MESSAGE, "删除成功！");
		}
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.print(JsonUtil.object2json(map));
		out.close();
	}
	
}



